﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RestaurantPicker
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        DataBaseConnection db = new DataBaseConnection();

        public MainWindow()
        {
            InitializeComponent();
        }

        // On application load
        private void OnLoad(object sender, RoutedEventArgs e)
        {
            try
            {
                db.OpenConnection();
                List<string> list = db.LoadList();
                foreach (string restaurant in list)
                {
                    restListBox.Items.Add(restaurant);
                }
            }
            catch
            {
                MessageBox.Show("Could not access Restaurants database...closing!");
                Application.Current.Shutdown();
            }
        }

        // On application close
        private void OnClose(object sender, EventArgs e)
        {
            db.CloseConnection();
        }

        // Add restaurant to left list and database
        private void addBtn_Click(object sender, RoutedEventArgs e)
        {
            if (addBox.Text.Length > 0)
                try
                {
                    db.Add(addBox.Text);
                    restListBox.Items.Add(addBox.Text);
                }
                catch
                {
                    MessageBox.Show("This restaurant already in the list!");
                }
            addBox.Clear();
        }

        // Delete restaurant from left list and database
        private void delBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                db.Delete(restListBox.SelectedItem.ToString());
                restListBox.Items.Remove(restListBox.SelectedItem.ToString());
            }
            catch{ }
        }

        // Close the application
        private void exitBtn_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        // Pick random restaurant from right list
        private void randomBtn_Click(object sender, RoutedEventArgs e)
        {
            if (restAddedListBox.Items.Count > 0)
            {
                Random rnd = new Random();
                int pick = rnd.Next(0, restAddedListBox.Items.Count);
                MessageBox.Show(restAddedListBox.Items.GetItemAt(pick).ToString());
            }
            else
            {
                MessageBox.Show("Add restaurants to choose from to the right!");
            }
        }

        // Move restaurant from left list to right list
        private void moveToListBtn_Click(object sender, RoutedEventArgs e)
        {
            if (restListBox.SelectedItem != null)
            {
                restAddedListBox.Items.Add(restListBox.SelectedItem);
                restListBox.Items.Remove(restListBox.SelectedItem);
            }
        }

        // Move restaurant from right left to left list
        private void removeFromListBtn_Click(object sender, RoutedEventArgs e)
        {
            if (restAddedListBox.SelectedItem != null)
            {
                restListBox.Items.Add(restAddedListBox.SelectedItem);
                restAddedListBox.Items.Remove(restAddedListBox.SelectedItem);
            }
        }

    }
}